import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {

    path: "/login",
    component: () => import('../views/login.vue')
  },
  {
    path: '/layout',
    component: () => import('../views/layout.vue'),
    children: [
      // 显示首页模块
      {
        path: "/index",
        component: () => import('../views/index.vue')
      },
      {
        path: "/menu",
        component: () => import('../views/menu.vue')
      },
      {
        path: '/role',
        component: () => import('../views/role.vue')
      },
      {
        path:'/user',
        component:()=>import('../views/admin.vue')
      },
      {
        path:'/category',
        component:()=>import('../views/points.vue')
      },
      {
        path:'/specs',
        component:()=>import('../views/rules.vue')
      },
      {
        path:"/goods",
        component:()=>import('../views/goods.vue')
      }
    ]
  }
]

const router = new VueRouter({
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  var luyou = ['/login']
  if (luyou.indexOf(to.path) === -1) {
    //  拿到token参数
    let tokens = localStorage.getItem('loginture') || '{}'
    let tokenTF = JSON.parse(tokens).token
    if (tokenTF) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})
export default router
